/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.calendarplan;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.account.SysUser;
import com.unis.model.calendarplan.Calendarplan;
import com.unis.service.calendarplan.CalendarplanService;

/**
 * @author Administrator (email:357375084@qq.com)
 * @since 2013-02-27
 */
@Controller
@RequestMapping("/calendarplan")
public class CalendarplanController {

    private CalendarplanService calendarplanService;

    /****************************************
     * Insert a instace of Calendarplan
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertCalendarplan(@RequestBody Calendarplan calendarplan) throws Exception {
        calendarplan.setStatmentId("Calendarplan.insert");
        //暂时只用用户名ID暂时空缺
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        calendarplan.setAssignername(user.getUsername());
        calendarplan.setAssignerid(user.getUserId());
        calendarplan.setStatus(0);
        String status = "failure";
        calendarplanService.insert(calendarplan);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Calendarplan
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Calendarplan calendarplan) throws Exception {
        calendarplan.setStatmentId("Calendarplan.update");
        //暂时只用用户名ID暂时空缺
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        calendarplan.setAssignername(user.getUsername());
        calendarplan.setAssignerid(user.getUserId());
        String status = "failure";
        calendarplanService.update(calendarplan);
        status = "success";
        return status;
    }
    
    /****************************************
     * Update a instace of Calendarplan(只修改状态字段)
     ****************************************/
    @RequestMapping("/finish")
    @ResponseBody
    public Object finish(@RequestBody Calendarplan calendarplan) throws Exception {
        calendarplan.setStatmentId("Calendarplan.finish");
        calendarplan.setStatus(1);
        String status = "failure";
        calendarplanService.update(calendarplan);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Calendarplan
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Calendarplan calendarplan) throws Exception {
        calendarplan.setStatmentId("Calendarplan.delete");
        String status = "failure";
        calendarplanService.batchDelete(calendarplan);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Calendarplan
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Calendarplan calendarplan = new Calendarplan();
        calendarplan.setStatmentId("Calendarplan.queryAsList");
        BeanUtils.populate(calendarplan, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(calendarplan, pageMap);
        List<BaseModel> calendarplanList = calendarplanService.queryAsList(calendarplan);
        Map<String, Object> renderMap = GridUIMapper.render(calendarplan, calendarplanList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Calendarplan
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Calendarplan calendarplan) throws Exception {
        calendarplan.setStatmentId("Calendarplan.queryAsObject");
        return calendarplanService.queryAsObject(calendarplan);
    }

    @RequestMapping("/page")
    public String forword() {
        return "calendarplan/calendarplanList";
    }

    @Resource
    public void setCalendarplanService(CalendarplanService calendarplanService) {
        this.calendarplanService = calendarplanService;
    }
}
